package com.kb.service.impl.iems;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.kb.common.MeException;
import com.kb.dto.iems.OrderBomErrorDetailRecordReplyDTO;
import com.kb.service.EngineeringService;
import com.kb.vo.iems.EmailOrderBomDetailErrorRecordVO;
import com.kb.vo.iems.RemoteEmailOrderBomDetailErrorRecordResultVO;
import com.kb.vo.iems.RemoteOrderBomDetailErrorRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class EngineeringServiceImpl implements EngineeringService {
    @Override
    public EmailOrderBomDetailErrorRecordVO queryEmailOrderBomDetailErrorRecordVO(String id) {
        HttpResponse response = HttpRequest.get("http://192.168.10.24:48081/admin-api/info-manage-api/orderBomErrorRecord/open/email/detail/" + id)
                .execute();

        RemoteEmailOrderBomDetailErrorRecordResultVO bean = JSONUtil.toBean(response.body(), RemoteEmailOrderBomDetailErrorRecordResultVO.class);
        if (Objects.isNull(bean) || !Objects.equals(bean.getCode(), 0) || Objects.isNull(bean.getData())) {
            log.error("调用远程queryEmailOrderBomDetailErrorRecordVO接口报错{}", bean.toString());
            throw new MeException("调用远程接口报错");
        }
        return bean.getData();
    }

    @Override
    public Boolean replyOrderBomDetailErrorRecord(OrderBomErrorDetailRecordReplyDTO dto) {
        HttpResponse response = HttpRequest.post("http://192.168.10.24:48081/admin-api/info-manage-api/orderBomErrorRecord/open/email/reply")
                .header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(dto))
                .execute();

        RemoteOrderBomDetailErrorRecordVO bean = JSONUtil.toBean(response.body(), RemoteOrderBomDetailErrorRecordVO.class);
        if (Objects.isNull(bean) || !Objects.equals(bean.getCode(), 0) || Objects.isNull(bean.getData())) {
            log.error("调用远程查询replyOrderBomDetailErrorRecord接口报错{}", bean.toString());
            throw new MeException("调用远程接口报错");
        }
        return bean.getData();
    }
}
